//
//  FKTeamMemberVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/14.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKTeamMemberVC.h"
#import <JXCategoryView.h>
#import "FKMemberChildVC.h"

@interface FKTeamMemberVC ()<JXCategoryViewDelegate>

@property (nonatomic, weak) UISegmentedControl *segment;
@property (nonatomic, weak) JXCategoryTitleView *categoryView;

@end

@implementation FKTeamMemberVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"直邀", @"间邀"]];
    self.navigationItem.titleView = segment;
    self.segment = segment;
    [segment addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
    segment.selectedSegmentIndex = 0;
    segment.tintColor = fkMainColor;
    segment.fk_width = fk_adjustW(200);
    
    NSArray *titles = @[@"有效会员(0人)", @"非有效会员(0人)"];
    
    // 默认为user类型
    CGFloat headerH = fk_adjustW(44);
    JXCategoryTitleView *categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, headerH)];
    [self.view addSubview:categoryView];
    
    UIView *separator = [UIView fk_lineWithFrame:CGRectMake(0, headerH - 0.8, fkScreenW, 0.8)];
    [self.view addSubview:separator];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    scrollView.pagingEnabled = true;
    scrollView.showsHorizontalScrollIndicator = false;
    scrollView.bounces = false;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(separator.mas_bottom);
        make.left.bottom.right.offset(0);
    }];
    
    self.categoryView = categoryView;
    categoryView.titleSelectedColor = fkMainColor;
    categoryView.delegate = self;
    categoryView.titleColorGradientEnabled = YES;
    categoryView.titles = titles;
    categoryView.backgroundColor = [UIColor whiteColor];
    
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    categoryView.indicators = @[lineView];
    lineView.indicatorLineViewColor = fkMainColor;
    
    categoryView.contentScrollView = scrollView;
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag | UIScrollViewKeyboardDismissModeInteractive;
    
    CGFloat scrollH = fkScreenH - fkStatusAndNaBarH - headerH;
    NSArray *types = @[@"31", @"32"];
    for (NSInteger index = 0; index < titles.count; index++) {
        // FKConsultListVC不能继承自UITableViewController，否则会导致宽度无法改变
        FKMemberChildVC *vc = [[FKMemberChildVC alloc] init];
        [self addChildViewController:vc];
        vc.type = types[index];
        vc.view.frame = CGRectMake(fkScreenW * index, 0, fkScreenW, scrollH);
        [scrollView addSubview:vc.view];
        
        fkWeakSelf(self);
        vc.updateInviteNum = ^(NSDictionary * _Nonnull dict) {
            if (dict.count == 2) {
                NSString *leftTitle = fkFORMAT(@"有效会员(%@人)", dict[@"invite_num_valid"]);
                NSString *rightTitle = fkFORMAT(@"非有效会员(%@人)", dict[@"invite_num_invalid"]);
//                [weakself.categoryView upda];
                weakself.categoryView.titles = @[leftTitle, rightTitle];
                [weakself.categoryView reloadData];
            }
        };
        
        if (index == 0) [vc refreshData];
    }
    scrollView.contentSize = CGSizeMake(fkScreenW * titles.count, 0);
}

- (void)segmentValueChanged:(UISegmentedControl *)segment
{
    NSInteger cateIndex = self.categoryView.selectedIndex;
    FKMemberChildVC *vc = self.childViewControllers[cateIndex];
    vc.type = [self changeTypeByIndex:cateIndex];
    [vc.tableView.mj_header beginRefreshing];
}


#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index
{
    FKMemberChildVC *vc = self.childViewControllers[index];
    vc.type = [self changeTypeByIndex:index];
    [vc.tableView.mj_header beginRefreshing];
}

- (NSString *)changeTypeByIndex:(NSInteger)index
{
    if (self.segment.selectedSegmentIndex == 0) {
        if (index == 0) {
            return @"31";
        } else {
            return @"32";
        }
    } else {
        if (index == 0) {
            return @"41";
        } else {
            return @"42";
        }
    }
}


@end
